#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <Stdbool.h>
typedef struct {
    int* _a;
    int Front;
    int Rear;
    int size;
    
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
   MyCircularQueue* q=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
   q->_a=(int*)malloc(sizeof(int)*(k+1));
   q->Front=0;
   q->Rear=0;
   q->size=k;
   return q;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    if(obj->Front==obj->Rear)
        return true;
    else
        return false;
}   

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    if((obj->Rear+1)%(obj->size+1)==obj->Front)
        return true;
    else
        return false;
   
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if(myCircularQueueIsFull(obj))
        return false;
    else
        {
            obj->_a[obj->Rear]=value;
            obj->Rear++;
            obj->Rear%=obj->size+1;//因为队列的下标是从0开始的所以要取k+1的模
            return true;
        }
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
        return false;
    else
    {
        obj->Front++;
        obj->Front%=obj->size+1;
        return true;
    }
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
        return -1;
    else
    {
        return obj->_a[obj->Front];
    }
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
        return -1;
    else
    {
        int rear=obj->Rear-1;
        if(rear==-1)
            rear=obj->size;
        return obj->_a[rear];
    }     
}



void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->_a);
    obj->_a=NULL;
    free(obj);
}
